package com.diudiutechnology.www.yadiudiu.util.thirdpay;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.alipay.sdk.app.PayTask;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;


import java.util.Map;

public class AlipayClass {
    /**
     * 支付宝支付业务：入参app_id
     */
    public static final String APPID = "2017041006619216";
    //http://www.baidu.com
/*    // 商户PID
    public static final String PARTNER = "2088621602116193";*/
    // 商户收款账号
    public static final String SELLER = "diudiu2080217058@sina.com";
    // 商户私钥，pkcs8格式
    public static final String RSA_PRIVATE = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIeaW3BL5JgblDIBDCec3JAfiWs88agOcQ" +
            "zUOXlVHncFAtFyJjsJLV2+DV32ASog2Wk5Dh+KcbefEg/dtHx/y8Tgt++zfm6eTo/KHwgka+SiGQNs8nvldeYwXu1nQ3xDhGfGBJL7F0PXBkALoJNQE" +
            "6xVM5n/KeSzZfESYcPARXRlAgMBAAECgYAsrI7JXUdvUU6udqfheQ01Mh6cxNQ8cJ2QA2736TIDNhRqmje8JVbb1lwjA4P4tB/MSa68d2jlfLN/pKPs" +
            "jFG+CxcFeSnpvnVBy27m4Eg7CAQ+8M+1bKluWu6hNXUSw5oLFEWOdTX5hOlU2XjHRo9t4H+LWUZPwN64qbrp68EuVQJBAN+TAg8zgVDE8yZZ+KuCCGH" +
            "Hq9vpiKw9207gJv+TSlnEstB2CgyZH/9w36ytk5g7u+85msmuGUaV+rg5m28G4NsCQQCbRRnXeU/UE5klSPy/jhXHCAUpQbGjoqWPbDSi+Ahb3211gft" +
            "UJdJsQfFq2JK28iY0kHYfciGJ7VA7sJ8XPWO/AkBY+uYv7JzD646mLdq1T6xMVi6E+J4HhxrYeoeq4hEORyWvp7r6JptAi8jJrc93IBb879019U9j817" +
            "0oSDGgGNPAkBn0qba6+UzG/+1bL6En890DKrW0XSkxGcMJgB0UIHWXOnmPwn9VGUFunphRXulFPny//3cPQasIaEaqJlYgtYlAkEAxwPw51+P2Wjcq0b" +
            "c6377mWlMUtiQpORY/ABBsu8bbon/kweveCr4V952MAWNLHQqSPZrtBVO1y1RND64+8UwTA==";
    private static final int SDK_PAY_FLAG = 1;

    private Context context;
    //    private AlipayListener alipayListener;
    private String goodsName;
    private String goodsDesc;
    private String orderId;
    private Double price;
    private String callBackUrl;

    /**
     * @param context
     * @param goodsName   订单信息
     * @param goodsDesc   商品描述
     * @param orderId     订单Id
     * @param price       钱
     * @param callBackUrl
     */
    public AlipayClass(Context context, String goodsName, String goodsDesc, String orderId, Double price, String
            callBackUrl) {
        this.context = context;
        this.goodsName = goodsName;
        this.goodsDesc = goodsDesc;
        this.orderId = orderId;
        this.price = price;
        this.callBackUrl = callBackUrl;
//        this.alipayListener = alipayListener;
        pay();
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
//                        ((HelpPeopleActivity)context).getcehnggou();

//                        alipayListener.alipaySuccess();
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        ToastUtils.show(context, "支付失败~~~~~~~~");
//                        alipayListener.alipayError();
                    }
                    break;
                }
                default:
                    break;
            }
        }

        ;
    };

    /**
     * call alipay sdk pay. 调用SDK支付  V2............170426号
     */
    public static final String RSA2_PRIVATE = "";

    public void pay() {
        /**V2*/
        boolean rsa2 = (RSA2_PRIVATE.length() > 0);
        Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(APPID, rsa2, goodsName, goodsDesc, orderId,
                String.valueOf(price), callBackUrl);
        String orderParam = OrderInfoUtil2_0.buildOrderParam(params);

        String privateKey = rsa2 ? RSA2_PRIVATE : RSA_PRIVATE;
        String sign = OrderInfoUtil2_0.getSign(params, privateKey, rsa2);

        final String orderInfo = orderParam + "&" + sign;
        /**V2*/
        Runnable payRunnable = new Runnable() {

            @Override
            public void run() {
                PayTask alipay = new PayTask((Activity) context);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Log.i("msp", result.toString());

                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };

        // 必须异步调用
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }
}
